<?php

namespace App\Http\Controllers;

use App\Models\Statistics;
use Illuminate\Http\Request;

class StatisticsController extends Controller
{
    protected   $model;

    public function __construct(Statistics $statistics)
    {
        $this->model = $statistics;
    }

    public function index(Request $request)
    {

        $osArr = ['Android' , 'iPhone' . 'iPad'];

        $userAgent = $request->server('HTTP_USER_AGENT');


        for ($i=0; $i <count($osArr) ; $i++) {
            if ( strpos($userAgent, $osArr[$i]) !== false ) {
                $os = $osArr[$i];
            }else{
                $os = 'PC';
            }
        }



        $redirect = $request->input('redirect');

        $params['web_id'] = $request->input('id');
        $params['ip'] = ($request->ip() == '::1') ? ip2long('127.0.0.1') : ip2long($request->ip());
        $params['os'] = $os;

        $this->model->create($params);


        return  redirect($redirect);



    }
}
